/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.main;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.activities.maps.MapWhereAreTheyActivity;
import com.phonebooksharing.android.util.Global;

public class MapContainerActivity extends ExpandableListActivity {
//    private TextView emptyText;
//    private ProgressBar emptyPG;
//    private AsyncTask<?, ?, ?> task;
    
    private static final int /*FRIENDS_NEARBY = 0,*/
                             WHERE_ARE_THEY = 1;
    
    private DashboardExpandableAdapter m_adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        setContentView(R.layout.list_layout_expand_with_empty);
        getExpandableListView().setDivider(getResources().getDrawable(R.drawable.black_bar));
        
        buildDashboard();
    }

    private void buildDashboard() {
        ArrayList<Group> groups = new ArrayList<Group>();
        
//        addNearby(groups);
        addContacts(groups);

        m_adapter = null;
        m_adapter = new DashboardExpandableAdapter(this, groups);
        setListAdapter(m_adapter);
        
        ExpandableListView elv = getExpandableListView();
        int i = 0;
        for(Group g : groups) {
            if(g.expand) elv.expandGroup(i);
            i++;
        }
    }

//    private Dashboard dashboard;
//    private void addNearby(ArrayList<Group> groups) {
//        dashboard = PhonebookSharingManager.getDashboard(getContentResolver(), Dashboard.CONTENT_URI);
//        final ArrayList<Child> child = new ArrayList<Child>();
//        int i, count = 0;
//        if ((i = dashboard.qtyFriendsNearby) > 0) {
//            Child ch = new Child();
//            ch.icon = R.drawable.dash_friends_nearby;
//            ch.title = "Contacts Nearby";
//            ch.small = "(" + i + " items)";
//            ch.desc = "Check where your contacts are";
//            ch.what = FRIENDS_NEARBY;
//
//            count += i;
//            child.add(ch);
//        }
//        
//        groups.add(new Group("Nearby", child, count));
//    }

    private void addContacts(ArrayList<Group> groups) {
        final ArrayList<Child> child = new ArrayList<Child>();
        int count = 0;

        Child ch = new Child();
        ch.icon = R.drawable.dash_messages;
        ch.title = "Where are they?";
        ch.small = "";
        ch.desc = "Check where your friends are";
        ch.what = WHERE_ARE_THEY;

        count += 1;
        child.add(ch);

        Group inbox = new Group("Where are they?", child, count);
        inbox.expand = true;
        groups.add(inbox);
    }
    
    static class Group {
        Group(String n, ArrayList<Child> childs, long count) {
            this.name = n;
            this.count = count;
            this.childs = childs;  
        }
        boolean expand; //TODO: make me a preference
        String name;
        long count;
        ArrayList<Child> childs;
    }
    
    static class Child {
        int icon, what;
        String title, small, desc;
    }
    
    static class DashboardExpandableAdapter extends BaseExpandableListAdapter {
        private ArrayList<Group> groups;
        private LayoutInflater inflater;

        public DashboardExpandableAdapter(Context c, ArrayList<Group> groups) {
            this.groups = groups;
            this.inflater = LayoutInflater.from(c);
        }

        @Override
        public Object getChild(int gp, int cp) {
            return groups.get(gp).childs.get(cp);
        }

        @Override
        public long getChildId(int gp, int cp) {
            return groups.get(gp).childs.get(cp).what;
        }

        @Override
        public View getChildView(int gp, int cp, boolean isLastChild, View cv, ViewGroup parent) {
            ViewHolder holder;
            
            Group g = groups.get(gp);
            Child c = g.childs.get(cp);
            if(cv == null) {
                cv = inflater.inflate(R.layout.list_item_3lines, parent, false);
                holder = new ViewHolder();
                holder.icon = (ImageView)cv.findViewById(android.R.id.icon);
                holder.text1 = (TextView)cv.findViewById(android.R.id.text1);
                holder.text2 = (TextView)cv.findViewById(R.id.text2);
                holder.text3 = (TextView)cv.findViewById(R.id.text3);

                cv.setTag(holder);
            } else {
                holder = (ViewHolder)cv.getTag();
            }
            
            holder.icon.setImageResource(c.icon);
            holder.text1.setText(c.title);
            holder.text2.setText(c.small);
            holder.text3.setText(c.desc);
            
            return cv;
        }

        @Override
        public int getChildrenCount(int gp) {
            if( gp >= groups.size()){
                return 0;
            }
            return groups.get(gp).childs.size();
        }

        @Override
        public Object getGroup(int gp) {
            return groups.get(gp);
        }

        @Override
        public int getGroupCount() {
            return groups.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            Group g = groups.get(groupPosition);
            View rv;
            if (convertView == null) {
                rv = inflater.inflate(R.layout.exp_2column, null);
            } else {
                rv = convertView;
            }
            ((TextView) rv.findViewById(android.R.id.text1)).setText(g.name);
//            ((TextView) rv.findViewById(android.R.id.text2)).setText("(" + g.count + " items)");
            return rv;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
        
        static class ViewHolder {
        	ImageView icon;
        	TextView text1, text2, text3;
        }
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        return new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Choose accuracy in " + (Global.unitValue == 1 ? "Meters" : "Miles"))
        .setItems(R.array.pref_friend_accuracy, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                final int[] items = getResources().getIntArray(R.array.pref_friend_accuracy);
                
                Intent i = new Intent(MapContainerActivity.this, MapWhereAreTheyActivity.class);
                i.putExtra(MapWhereAreTheyActivity.ACCURACY, items[whichButton]);
                startActivity(i);
            }
        })
       .create();
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int gp, int cp, long id) {
//        if(gp == 0) {//nearby and inbox
//            switch ((int)id) {
//                case FRIENDS_NEARBY: {
//                    showDialog(0);
//                    break;
//                } default:
//                    break;
//            }
//        } else if(gp == 1) {
            switch ((int)id) {
                case WHERE_ARE_THEY: {
                    Intent i = new Intent(this, MapWhereAreTheyActivity.class);
//                    i.putExtra(ImageGallerySharingContainer.INITIAL_TAB, 2);
                    startActivity(i);
                    break;
                }
            }
//        }
        
        return super.onChildClick(parent, v, gp, cp, id);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        menu.add(0, 0, 0, "Refresh").setIcon(R.drawable.ic_menu_refresh);
//        menu.add(1, 1, 1, "Preferences").setIcon(android.R.drawable.ic_menu_preferences);
//        return super.onCreateOptionsMenu(menu);
//    }
//    
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case 0: {//refresh
//                task = new RefreshTask().execute(this);
//                return true;
//            }
//            case 1: {//preferences
//                PreferencesActivity.show(this);
//                return true;
//            }
//        }
//        return false;
//    }
}
